package presentation;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.sql.Time;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import control.CControl;
import entity.ENode;
import entity.EPatternRegister;
import entity.EStringRegister;

public class PNodeErrorPanel extends JPanel implements Observer{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable p;
	private String[] nodes;
	private HashMap<String, Long> err;
	private DefaultTableModel m;
	
	public PNodeErrorPanel(CControl cControl,String end) {
		// TODO Auto-generated constructor stub
		this.setLayout(new BorderLayout());
		
		m = new DefaultTableModel(); 
		m.addColumn("Time");
		m.addColumn("Action");
		m.addColumn("Node");
		m.addColumn("Message");
		
		//m.addRow(new Object[]{new Date().toString(), "set", "MBN","Fault in slecting the right state reakfbalkjfgaækjfnglajbrvlanrvalrfnalkjdflakjdfhlakjdhflkjadhflkajhdlfkjh"});
		
		p = new JTable();
		p.setAutoCreateColumnsFromModel(true);
		p.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		
		p.setModel(m);
		
		Enumeration<TableColumn> d = p.getColumnModel().getColumns();
		
		int j = 0;
		while(d.hasMoreElements() && j < 4)
		{
			TableColumn cd = d.nextElement();
			if(j == 1 || j == 2)
			{
				cd.setMinWidth(30);
				cd.setMaxWidth(50);
			}
			if(j == 0)
			{
				cd.setMinWidth(200);
				cd.setMaxWidth(210);
			}
			j++;
		}
		
		this.add(p.getTableHeader(),BorderLayout.NORTH);
		JScrollPane scbar = new JScrollPane(p);
;		this.add(scbar,BorderLayout.CENTER);
		
		err = new HashMap<String, Long>();
		
		nodes = cControl.getNodeList();
		
		for(int i =0 ; i<nodes.length;i++)
		{
			if(nodes[i].equalsIgnoreCase("MN"))
			{
				
			}
			else
			{
				cControl.addRegisterObserver(nodes[i], nodes[i] + end, this);
				err.put(nodes[i] + end,(long)0);
			}
		}
		
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		if(o instanceof EPatternRegister)
		{
		EPatternRegister e = (EPatternRegister) o;
		
		// read current err val
		long old_err = err.get(e.getName());
		long new_err = Long.parseLong((String)arg, 2);
		// get difference 
		long diff = new_err ^ old_err;
		long changed_set = diff & new_err;
		long cleared_set = diff & (~new_err);
		err.put(e.getName(), new_err);
		if(changed_set != 0)
		{
			Date date = new Date();
			
			String[] d = e.getTextValue(changed_set);
			for(int i=0;i<d.length;i++)
			{
				if(d[i] != null && !d[i].isEmpty())
				{
					m.addRow(new Object[]{date.toString(), "set", e.getName(),d[i]});
				}
			}
		}
		if(cleared_set != 0)
		{
			Date date = new Date();
			
			String[] d = e.getTextValue(cleared_set);
			for(int i=0;i<d.length;i++)
			{
				if(d[i] != null && !d[i].isEmpty())
				{
					m.addRow(new Object[]{date.toString(), "clr", e.getName(),d[i]});
				}
			}
			
		}
		}
	}

}
